package com.tz.system.role.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.tz.platform.page.PageCondition;
import com.tz.platform.utils.StringUtil;
import com.tz.system.menu.model.TSysMenu;
import com.tz.system.menu.service.TSysMenuService;
import com.tz.system.role.model.TMgtRole;
import com.tz.system.role.model.TMgtRolePrivate;
import com.tz.system.role.service.RolePrivateService;
import com.tz.system.role.service.TMgtRoleService;



@Controller
@RequestMapping("/system/role/tMgtRole")
public class TMgtRoleController {
	
	@Autowired
	private TMgtRoleService tMgtRoleService;
	
	@Autowired
	private RolePrivateService rolePrivateService;
	
	@Autowired
	private TSysMenuService tSysMenuService;
	
	/**
	 * 跳转到列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/list")
	@ResponseBody
	public ModelAndView list(PageCondition pl, Model model, HttpServletRequest request) {
		//设置页面属性
		model.addAllAttributes(pl.getParamsCondition());
		// 2、执行查询
		pl = tMgtRoleService.findByPage(pl);
		// 3、设置查询结果
		model.addAttribute("pl", pl);
		return new ModelAndView("/system/role/t-mgt-role-list");
	}
	
	/**
	 * 查询列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toAddPage")
	public ModelAndView toAddPage(Model model,String id) {
		if(!StringUtil.msIsEmpty(id)){
			TMgtRole entity = tMgtRoleService.queryById(Integer.parseInt(id));
			model.addAttribute("entity", entity);
		}
		return new ModelAndView("/system/role/t-mgt-role-form");
	}
	
	/**
	 * 增加信息
	 * @param entity
	 * @return
	 */
	@RequestMapping(value = "/addSave")
	@ResponseBody
	public Map<String,Object> addSave(TMgtRole entity) {
		return tMgtRoleService.addSave(entity);
	}
	
	/**
	 * 更新信息
	 * @return
	 */
	@RequestMapping(value = "/updateSave")
	@ResponseBody
	public Map<String,Object> updateSave(TMgtRole entity) {
		return tMgtRoleService.updateSave(entity);
	}
	
	/**
	 * 根据id删除信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/deleteObj")
	@ResponseBody
	public Map<String,Object> deleteTMgtRole(String id) {
		return tMgtRoleService.removeById(Integer.valueOf(id));
	}
	
	
	/**
	 * 查询列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/toSetPage")
	public ModelAndView toSetPage(Model model,String roleCode) {
		 // 1、查询出角色信息
	    TMgtRole role = tMgtRoleService.queryByRoleCode(roleCode);
	    if( role == null ){
	      role = new TMgtRole();
	    }
	    model.addAttribute("role", role);
	    // 2、该角色的权限列表
	    List<TMgtRolePrivate> rolePrivateList = rolePrivateService.getListByRoleCode(roleCode);
	    // 3、显示全部菜单列表
	    model.addAttribute( "rolePrivates",rolePrivateList);
	    List<TSysMenu> menus = tSysMenuService.listAllMenus();
	    model.addAttribute( "menus",menus);
		return new ModelAndView("/system/role/t-mgt-role-set");
	}

	
	@RequestMapping("/saveRolePrivate")
	@ResponseBody
	public Map<String,Object> saveRolePrivate (Model model, String roleCode,String menusStr){
		return tMgtRoleService.saveRolePrivates(roleCode, menusStr);
	}
	
	
	
}

